package service;

import com.distribute.pojo.PageInfo;
import com.distribute.pojo.Result;
import com.distribute.pojo.enums.ServiceType;
import com.distribute.pojo.user.User;
import com.distribute.utils.JsonUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import constant.DSConstants;
import constant.MediaTypeConstants;
import core.api.ApiClient;
import exception.ApiException;
import service.api.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DSUserService implements UserService {
    private final ApiClient apiClient;

    public DSUserService(ApiClient apiClient) {
        this.apiClient = apiClient;
    }
    @Override
    public ServiceType getServiceType() {
        return ServiceType.USER;
    }

    @Override
    public Boolean verifyUserName(String userName) {
        if(userName == null || userName.isEmpty()){
            throw new ApiException("userName is null");
        }
        Map<String, String> body = new HashMap<>();
        body.put(DSConstants.USER_NAME, userName);
        Result result = apiClient.get(DSConstants.USER_VERIFY_USER_NAME_PATH, Result.class, userName.trim());
        if(result.isFailed()){
            throw new ApiException(result.getMsg());
        }
        return result.isSuccess();
    }

    @Override
    public User createUser(String userName, String userPassword, Integer tenantId, String email, String queue, String phone, Integer state) {
        if(!verifyUserName(userName)){
            throw new ApiException("userName verify is failed");
        }
        Map<String, Object> body = new HashMap<>();
        body.put(DSConstants.USER_NAME, userName);
        body.put(DSConstants.USER_PASSWORD, userPassword);
        body.put(DSConstants.TENANT_ID, tenantId);
        body.put(DSConstants.EMAIL, email);
        body.put(DSConstants.QUEUE, queue);
        body.put(DSConstants.PHONE, phone);
        body.put(DSConstants.STATE, state);

        Result result = apiClient.post(DSConstants.USER_CREATE_PATH, body, MediaTypeConstants.MEDIA_TYPE_FORM, Result.class);
        if (result.isFailed()) {
            throw new ApiException(result.getMsg());
        }
        return JsonUtils.toBean(result.getData(), User.class);
    }

    @Override
    public User getUser(String userName) {
        if(userName == null || userName.isEmpty()){
            throw new ApiException("userName is null");
        }
        Result result = apiClient.get(DSConstants.USER_QUERY_PATH, Result.class, 10, 1, userName);
        if (result.isFailed()) {
            throw new ApiException(result.getMsg());
        }
        PageInfo pageInfo = JsonUtils.toBean(result.getData(), PageInfo.class);
        String content = JsonUtils.converString(pageInfo.getTotalList());
        List<User> users = JsonUtils.toList(content, new TypeReference<List<User>>() {
        });
        if (users != null && !users.isEmpty()) {
            return users.get(0);
        }else {
            return null;
        }
    }
}
